package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisUtil;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ProjectConstant;
import com.ruoyi.common.utils.parameter.SystemPushText;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.system.domain.UserSystemMsg;
import com.ruoyi.system.service.IUserSystemMsgService;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.mapper.UserMapper;

/**
 * 用户系统推送消息Controller
 * 
 * @author DEREK
 * @date 2023-05-31
 */
@RestController
@RequestMapping("/system/systemMsg")
public class UserSystemMsgController extends BaseController
{
    @Autowired
    private IUserSystemMsgService userSystemMsgService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户系统推送消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:systemMsg:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserSystemMsg userSystemMsg)
    {
        startPage();
        List<UserSystemMsg> list = userSystemMsgService.selectUserSystemMsgList(userSystemMsg);
        return getDataTable(list);
    }

    /**
     * 导出用户系统推送消息列表
     */
    @PreAuthorize("@ss.hasPermi('system:systemMsg:export')")
    @Log(title = "用户系统推送消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserSystemMsg userSystemMsg)
    {
        List<UserSystemMsg> list = userSystemMsgService.selectUserSystemMsgList(userSystemMsg);
        ExcelUtil<UserSystemMsg> util = new ExcelUtil<UserSystemMsg>(UserSystemMsg.class);
        util.exportExcel(response, list, "用户系统推送消息数据");
    }

    /**
     * 获取用户系统推送消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:systemMsg:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(userSystemMsgService.getById(id));
    }

    /**
     * 新增用户系统推送消息
     */
    @PreAuthorize("@ss.hasPermi('system:systemMsg:add')")
    @Log(title = "用户系统推送消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserSystemMsg userSystemMsg)
    {
    	userSystemMsg.setType(1);
    	userSystemMsg.setIcon(SystemPushText.BDAppPushIcon.SYSTEM_ICON);
    	boolean save = userSystemMsgService.save(userSystemMsg);
    	if(save) {
    		if(0 == userSystemMsg.getUserId().longValue()) {
    			List<Long> userIds = new ArrayList<Long>();
    			userIds.add(1L);
    			userIds.add(3L);
    			// 查询所有正常用户
    			List<User> userList = userMapper.selectList(new LambdaQueryWrapper<User>().in(User::getEnabled, userIds).isNotNull(User::getPhone)
    					.select(User::getId));
    			//redisUtil.sSet(ProjectConstant.REDIS_MSG_PREFIX, userList.stream().map(User::getId).collect(Collectors.toList()));
    			userList.stream().forEach(user ->{
    				redisUtil.sSet(ProjectConstant.REDIS_MSG_PREFIX, user.getId());
    			});
    		}else {
    			redisUtil.sSet(ProjectConstant.REDIS_MSG_PREFIX, userSystemMsg.getUserId());
    		}
    	}
        return toAjax(save);
    }

    /**
     * 修改用户系统推送消息
     */
    @PreAuthorize("@ss.hasPermi('system:systemMsg:edit')")
    @Log(title = "用户系统推送消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserSystemMsg userSystemMsg)
    {
        return toAjax(userSystemMsgService.updateById(userSystemMsg));
    }

    /**
     * 删除用户系统推送消息
     */
    @PreAuthorize("@ss.hasPermi('system:systemMsg:remove')")
    @Log(title = "用户系统推送消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userSystemMsgService.deleteUserSystemMsgByIds(ids));
    }
    
    public static void main(String[] args) {
		System.out.println(Md5Utils.hash("aa123456"));
	}
}
